package ch.reevolt.mosaic.fragments;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import ch.reevolt.android.graphics.layout.AspectGrid;
import ch.reevolt.mosaic.R;
import ch.reevolt.mosaic.Receiver;
import ch.reevolt.mosaic.Receiver.BroadcastListener;

public class RightView extends Fragment {

	AspectGrid grid;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.right_view, container, false);

		final ImageView i = (ImageView) view.findViewById(R.id.result);

		// i.setImageBitmap(createImage());

		Receiver receiver = new Receiver();
		getActivity().registerReceiver(receiver,
				new IntentFilter(ch.reevolt.mosaic.Receiver.ACTION_FRAGMENT));

		receiver.setListener(new BroadcastListener() {
			public void notify(Intent intent) {
				Bitmap imagePath = intent.getParcelableExtra("PATH");
				i.setImageBitmap(imagePath);
			}
		});

		return view;
	}

	public Bitmap createImage() {

		getActivity().setProgressBarIndeterminateVisibility(true);

		Paint mColor = new Paint(Paint.ANTI_ALIAS_FLAG);

		Bitmap render = Bitmap.createBitmap(500, 500, Bitmap.Config.ARGB_4444);

		Canvas c = new Canvas(render);

		for (int i = 0; i < 50; i++) {
			for (int j = 0; j < 50; j++) {
				c.drawBitmap(BitmapFactory.decodeResource(getResources(),
						R.drawable.ic_launcher), i * 50, j * 50, mColor);
			}
		}

		getActivity().setProgressBarIndeterminateVisibility(false);

		return render;

	}
}
